import tabbar from '../../utils/tabbar'
const app = getApp()
const api = wx.getStorageSync('api')
Page({
  data: {
    active: 1,
    navIndex: 0,
    navName: [],
    subCategory: []
  },

  // 获取所有分类
  getCategory() {
    let url = api.category
    app.$http.get(url).then(res => {
      console.log(res);
      let data = res.data.category
      let navName = []
      let subCategory = []
      data.forEach(item => {
        navName.push({
          text: item.name
        })
        subCategory.push(item.children)
      })
      this.setData({
        navName,
        subCategory
      })
    })
  },

  onLoad: function (options) {
    this.getCategory()
  },

  // 左侧nav点击
  onClickNav(e) {
    let navIndex = e.detail.index
    this.setData({
      navIndex
    })
  },

  // 下方tabbar点击
  onTabbarChange(e) {
    tabbar.redirectTo(e.detail)
  },

  // 前往搜索页
  toSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },

  gridItemClick(e) {
    let categoryId = e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: `/pages/goodsList/goodsList?keyword=${categoryId}&isCategory=1`
    })
  }
})